import java.util.HashMap;


public class GrapheTaquin {
	protected HashMap<Noeud, HashMap<Noeud, Integer>> successeurs;
	protected int dimension;
	protected int[] sequence;
	
	public GrapheTaquin(int n){
		 successeurs = new HashMap<Noeud, HashMap<Noeud, Integer>>();
		 dimension = n;
		 sequence = new int[n];
		 
	}

	public HashMap<Noeud, HashMap<Noeud, Integer>> getSuccesseurs() {
		return successeurs;
	}

	public void setSuccesseurs(HashMap<Noeud, HashMap<Noeud, Integer>> successeurs) {
		this.successeurs = successeurs;
	}

	public int getDimension() {
		return dimension;
	}

	public void setDimension(int dimension) {
		this.dimension = dimension;
	}

	public int[] getSequence() {
		return sequence;
	}

	public void setSequence(int[] sequence) {
		this.sequence = sequence;
	}
	
	public int[][] construireSuccesseur(int[] seq){
		
		
		int[][] newSequences = new int[4][dimension*dimension] ;
		for(int i =0; i<4; i++){
			//newSequences[i] = seq;
			for(int k=0; k<seq.length; k++){
				newSequences[i][k] = seq[k];
			}
		}
		
		for(int z=0; z<newSequences.length; z++){
			System.out.print("tableau "+z+ "-> ");
			for(int y=0; y < newSequences[z].length ; y++){
				System.out.print(newSequences[z][y]+" ");
			}
			System.out.println();
		}
		
		int i=0;
		while(i < seq.length && seq[i] != seq.length){
			i++;
		}
		
		int posTrou = i;
		
		int newPosTrou;
		
		newPosTrou = dimension*dimension + posTrou;
		newSequences[0] =permuter(newSequences[0], newPosTrou, posTrou);
		
		newPosTrou = dimension*dimension - posTrou;
		newSequences[1] = permuter(newSequences[1], newPosTrou, posTrou);
		
		newPosTrou = posTrou - 1;
		newSequences[2] = permuter(newSequences[2], newPosTrou, posTrou);
		
		newPosTrou = posTrou + 1;
		newSequences[3] = permuter(newSequences[3], newPosTrou, posTrou);
		
		return newSequences;
		
	}
	
	public int[] permuter(int[] seq,int a, int b){
		int temp;
		int[] resultat;
		System.out.println("Fonction permuter pour"+ a + " " + b);
		for(int i=0; i< seq.length; i++){
			System.out.print(seq[i]+ " ");
		}
		System.out.println();
		
		resultat = seq;
		if(a < seq.length && valide(a,b)){
			System.out.println("position possible");
			temp = resultat[a];
			resultat[a] = resultat[b];
			resultat[b] = temp;
		}
		
		for(int i=0; i< seq.length; i++){
			System.out.print(seq[i]+ " ");
		}
		System.out.println();
		
		return resultat;
	}
	
	private boolean valide(Integer p, int posTrou) {
		if(posTrou+dimension == p || posTrou-dimension == p || (posTrou+1 == p && p%dimension!=1) || (posTrou-1 == p && p%dimension!=1)){
			return true;
		}
		else{
			return false;
		}
	}
	

}
